package main

import (
	"fmt"
)

func main() {
	// 闭包：返回的匿名函数+匿名函数以外的变量num
	f := getSum()
	fmt.Println(f(1))
	fmt.Println(f(2))
	fmt.Println(f(3))
	fmt.Println(f(4))


	fmt.Println(getSum2(1))
	fmt.Println(getSum2(2))
	fmt.Println(getSum2(3))
	fmt.Println(getSum2(4))

}

// 求和(闭包)
func getSum() func(int) int  {
	var sum int = 0
	return func (num int) int {
		sum = sum + num
		return sum
	}
}

// 求和（不闭包）
func getSum2 (num int) int {
	var sum int = 0
	sum = sum + num
	return sum
}